<?php


namespace app\api\controller;


use app\common\lib\Show;
use app\common\business\Order as OrderBiz;
class Order extends AuthBase
{

    public function lists() {
        $pageSize = input('pageSize', 10, 'intval');
        $pageNum = input('pageNum', 1, 'intval');
        $res = (new OrderBiz())->getList($pageSize, $pageNum);
        return Show::success($res);
    }

    /**
     * 下单
     * @return \think\response\Json
     */
    public function makeOrder() {
        if (!$this->request->isPost()) {
            return show(config('status.error', '非法请求'));
        }
        //获取收货人信息
        $recevier_user = $this->request->param('recevier_user', '', 'trim');
        $recevier_phone = $this->request->param('recevier_phone', '', 'trim');
        $address = $this->request->param('address', '', 'trim');
        $postal_code = $this->request->param('postal_code', '', 'trim');
        if ($recevier_user == '' || $recevier_phone == '' || $address == '' || $postal_code == '') {
            return Show::error('参数错误');
        }
        $data = [
            'user_id' => $this->userId,
            'recevier_user' => $recevier_user,
            'recevier_phone' => $recevier_phone,
            'address' => $address,
            'postal_code' => $postal_code,
        ];
        try {
            $result = (new OrderBiz())->save($data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }
        if(!$result) {
            return Show::error("提交订单失败，请稍候重试");
        }
        return Show::success($result);
    }

    public function detail($id = 0) {
        $res = (new OrderBiz())->getDetail($this->userId, $id);
        return Show::success($res);
    }
}